//本源码为慕课网go语言体系课 其中一个项目：微商城项目
//视频地址    https://class.imooc.com/sale/go
//电子书地址  https://luboke.com/go
//版权申明    本源码为商业源码,为慕课网go语言体系课其中一个项目的源码，版权归【全栈编程@luboke.com】与【慕课网@imooc.com】 共同所有，尊重知识产权，欢迎正版购买，禁止盗版，盗版者将追究法律责任！
// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "carts": [{
      "id": "102",
      "num": "4",
      "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561280059656736.jpg",
      "pid": "8",
      "price": "5488.00",
      "pro_name": "【徕卡四摄 50倍数码变焦】Huawei/华为P30 Pro曲面屏超感光徕卡四摄变焦980芯片智能手"
    },
    {
      "uid": "5",
      "id": "103",
      "num": "1",
      "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561280311555874.jpg",
      "pid": "6",
      "price": "9999.00",
      "pro_name": "【6期免息】【买赠Freebuds2 Pro】Huawei/华为 Mate 20 RS 保时捷设计超",
      "uid": "5"
    },
    {
      "id": "104",
      "num": "1",
      "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561279221848907.jpg",
      "pid": "324",
      "price": "6188.00",
      "pro_name": "【下单立减800元】Apple/苹果 MacBook Air 13.3英寸128G 笔记本手提电脑 ",
      "uid": "5"
    }
    ],
    total:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.carts);
    this.setData({
      carts:this.data.carts,
    });
  },

  bindCheckbox:function(e) {
    var _index = parseInt(e.currentTarget.dataset.index);
    var _selected = this.data.carts[_index].selected;
    var carts = this.data.carts;
    carts[_index].selected = !_selected;

    this.setData({
      carts: carts,
    });

    // console.log(carts);
    this.sum();
  },

  sum: function(e) {
    console.log("执行开始");
    var _carts = this.data.carts;
    var _total = 0;
    for (var i = 0;i<_carts.length;i++) {
        if(_carts[i].selected) {
          _total += _carts[i].num * _carts[i].price
        }
    }

    console.log(_total);

    this.setData({
      total: _total,
    });
  },

// 减号函数
  bindMinus: function(e) {
    var _index = parseInt(e.currentTarget.dataset.index);  
    var _num = this.data.carts[_index].num;
    if(_num > 1) {
      _num --;
    }
    this.data.carts[_index].num = _num; 

    this.setData({
      carts: this.data.carts,
    });

    this.sum();
  },
// 加号函数
  bindPlus:function(e) {
    var _index = parseInt(e.currentTarget.dataset.index);
    var _num = this.data.carts[_index].num;
    _num ++;

    this.data.carts[_index].num = _num;
    this.setData({
      carts: this.data.carts,
    });
    this.sum();
  },
  // 从购物车中移除商品
  removeShopCard:function(e) {
    var _index = parseInt(e.currentTarget.dataset.index);

    var that = this;
    wx.showModal({
      title: '确定删除',
      content: '你真的要删除吗？',
      success:function(res) {          
          if(res.confirm) {
            console.log("移除之前：", that.data.carts)
            that.data.carts.splice(_index,1)
            console.log("移除之后：", that.data.carts)

            that.sum();

            that.setData({
              carts: that.data.carts,
            });
          } else {
            return false;
          }
      }
    })

    
  },
// 全选按钮
  bindSelectAll:function(e) {
    var _selectedAllStatus = this.data.selectedAllStatus;
    _selectedAllStatus = !_selectedAllStatus;

    var _carts = this.data.carts;
    for(var i=0;i<_carts.length;i++) {
      _carts[i].selected = _selectedAllStatus;
    }

    this.setData({
      selectedAllStatus: _selectedAllStatus,
      carts: _carts,
    });

    console.log(_carts);
    this.sum();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})